package cn.com.homeyoda.aimate.aihealth.ui.game;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import cn.com.homeyoda.aimate.aihealth.BR;
import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseActivity;
import cn.com.homeyoda.aimate.aihealth.databinding.ActivityGameBinding;
import cn.com.homeyoda.aimate.aihealth.ui.game.play.GamePlayActivity;
import cn.com.homeyoda.aimate.aihealth.ui.game.record.GameRecordActivity;

public class GameActivity extends BaseActivity<ActivityGameBinding, GameViewModel> implements View.OnClickListener {

    private GameAdapter gameAdapter;

    @Override
    public GameViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getApplication());
        return ViewModelProviders.of(this, factory).get(GameViewModel.class);
    }

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_game;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        binding.ivBack.setOnClickListener(this);
        binding.tvRecord.setOnClickListener(this);

        viewModel.getGameData();

        initRecyclerView();
    }

    private void initRecyclerView() {

        gameAdapter = new GameAdapter(R.layout.game_item);
        binding.setLayoutManager(new LinearLayoutManager(this));
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.divider_transparent));
        binding.rvGame.addItemDecoration(divider);
        binding.setAdapter(gameAdapter);

        gameAdapter.setOnItemChildClickListener((adapter, view, position) ->
                startActivity(new Intent(GameActivity.this, GamePlayActivity.class)));
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.gameData.observe(this, list -> gameAdapter.setNewData(list));
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.iv_back) {
            finish();
        } else if (v.getId() == R.id.tv_record) {
            startActivity(new Intent(this, GameRecordActivity.class));
        }
    }

}